/**
 *
 */
package com.perselab.recipe.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.security.core.GrantedAuthority;

/**
 * @author lewin.nguyen This class is used to represent available roles in the
 *         database.
 */
@Entity
@Table(name = "role")
public class Role extends AbstractModel implements GrantedAuthority {
	private static final long serialVersionUID = 2382811635458221331L;
	
	private Long id;
	private String name;
	private String description;

	/**
	 * Default constructor - creates a new instance with no values set.
	 */
	public Role() {
	}

	/**
	 * Create a new instance and set the name.
	 *
	 * @param name
	 *            name of the role.
	 */
	public Role(final String name) {
		this.name = name;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return id;
	}

	/**
	 * @see org.acegisecurity.GrantedAuthority#getAuthority()
	 * @return the name property (getAuthority required by Acegi's
	 *         GrantedAuthority interface)
	 */
	@Transient
	public String getAuthority() {
		return getName();
	}

	/**
	 * @return
	 */
	@Column(length = 20)
	public String getName() {
		return this.name;
	}

	/**
	 * @return
	 */
	@Column(length = 64)
	public String getDescription() {
		return this.description;
	}

	/**
	 * @param id
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @param name
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @param description
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (!(o instanceof Role)) {
			return false;
		}

		final Role role = (Role) o;

		return !(name != null ? !name.equals(role.name) : role.name != null);

	}

	/**
	 * {@inheritDoc}
	 */
	public int hashCode() {
		return (name != null ? name.hashCode() : 0);
	}

	/**
	 * {@inheritDoc}
	 */
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE).append(
				this.name).toString();
	}
}
